<?php

namespace Addons\Weixin;

use Common\Controller\Addon;

/**
 * 微信插件
 * @author eecjimmy
 */
define('WEIXIN_INCLUDE_PATH', ONETHINK_ADDON_PATH . 'Weixin/Include/');
import('fun', WEIXIN_INCLUDE_PATH, '.php');

class WeixinAddon extends Addon
{

    public $info = array(
        'name' => 'Weixin',
        'title' => '微信',
        'description' => '微信通用插件',
        'status' => 1,
        'author' => 'eecjimmy',
        'version' => '0.1'
    );

    public $admin_list = array(
        'model' => 'Example', //要查的表
        'fields' => '*', //要查的字段
        'map' => '', //查询条件, 如果需要可以再插件类的构造方法里动态重置这个属性
        'order' => 'id desc', //排序,
        'list_grid' => array( //这里定义的是除了id序号外的表格里字段显示的表头名和模型一样支持函数和链接
            'cover_id|preview_pic:封面',
            'title:书名',
            'description:描述',
            'link_id|get_link:外链',
            'update_time|time_format:更新时间',
            'id:操作:[EDIT]|编辑,[DELETE]|删除'
        ),
    );

    public $custom_adminlist = 'index.html';

    public function install()
    {
        $installSql = file_get_contents(WEIXIN_INCLUDE_PATH . 'install.sql');
        $arrSql = explode(';', $installSql);
        foreach ($arrSql as $v) {
            if (trim($v)) {
                $v = str_replace('{TABLEPREX}', C('DB_PREFIX'), $v) . ';';
                if (M()->execute($v) === false) {
                    session('addons_install_error', '：菜单表创建失败，请手动执行' . WEIXIN_INCLUDE_PATH . 'install.sql');
                    return false;
                }
            }
        }
        return true;
    }

    public function uninstall()
    {
        $uninstallSql = file_get_contents(WEIXIN_INCLUDE_PATH . 'uninstall.sql');
        $arrSql = explode(';' . PHP_EOL, $uninstallSql);
        foreach ($arrSql as $v) {
            if ($v) {
                $v = str_replace('{TABLEPREX}', C('DB_PREFIX'), $v);
                session('addons_install_error', '：菜单表删除失败，请手动执行' . WEIXIN_INCLUDE_PATH . 'uninstall.sql');
                if (M()->execute($v) === false) return false;
            }
        }
        return true;
    }

    //实现的app_begin钩子方法
    public function app_begin($param)
    {

    }

}